/**
 * @description 全局异常拦截器
 * @author hh
 */

import {
  ExceptionFilter,
  Catch,
  ArgumentsHost,
  HttpException,
} from '@nestjs/common';

import { Request, Response } from 'express';

@Catch(HttpException)
export class HttpFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const req = ctx.getRequest<Request>();
    const res = ctx.getResponse<Response>();
    const status = exception.getStatus();
    res.status(status).json({
      success: false,
      data: exception.message,
      code: 10000,
      status,
      path: req.url,
      method: req.method,
      timestamp: new Date(),
    });
  }
}
